package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxOpenAuthModel;
import com.wwdx.model.WwdxVipUserModel;
import com.wwdx.model.WwdxWeixinUserModel;
import com.wwdx.model.dto.MerchantInfoDTO;
import com.wwdx.service.WwdxWeixinUserServiceI;
import com.wwdx.utils.ConstantHolder;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author liguoliang
 */
@Service
public class WwdxWeixinUserServiceImpl extends BaseServiceImpl<WwdxWeixinUserModel> implements WwdxWeixinUserServiceI {

    @Override
    public WwdxWeixinUserModel getByOpenId(String openid) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("openid",openid);
        return getByHql("from WwdxWeixinUserModel where openid=:openid", params);
    }

    @Override
    public String getByToken(String token) {
        String key = String.format(ConstantHolder.USER_OBJ, token);
        return (String) valueOperations.get(key);
    }

    private String saveToken(String openId) {
        String token = super.getUUID().toUpperCase();
        //通过token保存用户信息
        String key = String.format(ConstantHolder.USER_OBJ, token);
        valueOperations.set(key,openId);
        //token有效期24小时
        redisTemplate.expire(key, 24 * 60 * 60, TimeUnit.SECONDS);
        //通过openId保存token信息
        valueOperations.set(String.format(ConstantHolder.USER_TOKEN, openId),token);
        return token;
    }

    private void execute(WwdxWeixinUserModel model,String merchantId){
        String openId = model.getOpenId();
        //获取令牌
        String token = this.saveToken(openId);
        model.setToken(token);
        //会员
        WwdxVipUserModel wwdxVipUserModel = wwdxVipUserService.getByOpenId(openId);
        if (wwdxVipUserModel != null){
            model.setVip(wwdxVipUserModel);
        }
        //检测商户是否绑定服务号
        WwdxOpenAuthModel wwdxOpenAuthModel = wwdxOpenAuthService.getByOpenIdAndStatus(openId, ConstantHolder.Constant.AUTHORIZED);
        if (wwdxOpenAuthModel != null){
            model.setAuthorizerAppid(wwdxOpenAuthModel.getAuthorizerAppid());
        }
        //商户信息
        if (StringUtils.isNotBlank(merchantId)){
            WwdxWeixinUserModel wwdxWeixinUserModel = this.getByOpenId(merchantId);
            if (wwdxWeixinUserModel != null){
                MerchantInfoDTO dto = new MerchantInfoDTO();
                dto.setHeadImgUrl(wwdxWeixinUserModel.getHeadImgUrl());
                dto.setNickname(wwdxWeixinUserModel.getNickname());
                model.setModel(dto);
            }
        }
    }

    @Override
    public WwdxWeixinUserModel saveOrUpdateWxMpUser(WxMpUser wxMpUser,String appId,String merchantId){
        logger.info("\n微信H5用户信息:{},appId:{},merchantId:{}", wxMpUser,appId,merchantId);
        WwdxWeixinUserModel model = this.getByOpenId(wxMpUser.getOpenId());
        if (model == null){
            model = new WwdxWeixinUserModel();
            BeanUtils.copyProperties(wxMpUser,model);
            model.setAppid(appId);model.setGmtCreated(new Date());
            model.setGmtModified(new Date());
            super.save(model);
        }else {
            model.setGmtModified(new Date());
            super.update(model);
        }
        this.execute(model,merchantId);
        return model;
    }
}